package util;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.firstapp.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Database {
    //创建ContentValues对象，用于后面操作数据库
    static ContentValues values = new ContentValues();

    /**
     * 添加数据的方法
     *
     * @param sqLiteDatabase 操作数据库的对象
     * @param table          要插入的表名
     * @param data           字段和内容的二维数组
     * @return 添加成功返回true, 否则返回false
     */
    public static boolean addDate(SQLiteDatabase sqLiteDatabase, String table, String[][] data) {
        values.clear();
        for (String[] datum : data) {
            values.put(datum[0], datum[1]);
        }
        long state;
        try {
            state = sqLiteDatabase.insert(table, null, values);
        } catch (Exception e) {
            Log.d("数据库操作", e.toString());
            return false;
        }
        return state != -1;
    }


    /**
     * 修改数据的方法
     *
     * @param sqLiteDatabase 操作数据库的对象
     * @param table          要操作的表名
     * @param data           字段和内容的二维数组
     * @param selection      筛选条件
     * @param selectionArgs  筛选条件的参数数组
     * @return 修改成功返回true，否则返回false
     */
    public static boolean updateDate(SQLiteDatabase sqLiteDatabase, String table, String[][] data,
                                     String selection, String[] selectionArgs) {
        values.clear();
        for (String[] datum : data) {
            values.put(datum[0], datum[1]);
        }
        long state;
        try {
            state = sqLiteDatabase.update(table, values, selection, selectionArgs);
        } catch (Exception e) {
            Log.d("数据库操作", e.toString());
            return false;
        }
        return state != 0;
    }


    /**
     * 删除数据的方法
     *
     * @param sqLiteDatabase 操作数据库的对象
     * @param table          要操作的表名
     * @param selection      筛选条件
     * @param selectionArgs  筛选条件的参数数组
     * @return 删除成功返回true，否则返回false
     */
    public static boolean deleteDate(SQLiteDatabase sqLiteDatabase, String table,
                                     String selection, String[] selectionArgs) {
        long state;
        try {
            state = sqLiteDatabase.delete(table, selection, selectionArgs);
        } catch (Exception e) {
            Log.d("数据库操作", e.toString());
            return false;
        }
        return state != 0;
    }


    /**
     * 查询数据库中的值，只返回一个值
     *
     * @param db            数据库对象
     * @param table         要查询的表名
     * @param column        字段名（不能为空或空字符串）
     * @param selection     筛选条件
     * @param selectionArgs 筛选条件的参数数组
     * @return 查询到的内容
     */
    @SuppressLint("Range")
    public static String select(SQLiteDatabase db, String table, String column,
                                String selection, String[] selectionArgs) {
        Cursor cursor = db.query(table, null, selection, selectionArgs,
                null, null, null);
        cursor.moveToFirst();
        String data = "";
        try {
            data = cursor.getString(cursor.getColumnIndex(column));
        } catch (Exception ignored) {
        }
        cursor.close();
        return data;
    }

    @SuppressLint("Range")
    public static List<Map<String, Object>> select_allPhone(SQLiteDatabase db, String table,
                                                            String selection, String[] selectionArgs, String orderBy) {
        Cursor cursor = db.query(table, null, selection, selectionArgs,
                null, null, orderBy);
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            while (cursor.moveToNext()) {
                Map<String, Object> m = new HashMap<>();
                m.put("ico", R.drawable.user);
                m.put("name", cursor.getString(cursor.getColumnIndex("name")));
                m.put("phoneNumber", cursor.getString(cursor.getColumnIndex("phoneNumber")));
                list.add(m);
            }
        } catch (Exception ignored) {
        }
        return list;
    }
}
